import 'package:flutter/material.dart';


Widget redBox = DecoratedBox(decoration: BoxDecoration(color: Colors.red));

class ConstrainedBoxDemo extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return Scaffold(

      appBar: AppBar(title: Text('尺寸类容器'),),

      ///可以限制子空间的最小宽度和高度
      body: Column(

        children: <Widget>[

          ConstrainedBox(

            ///BoxConstraints还定义了一些便捷的构造函数，用于快速生成特定限制规则的BoxConstraints，如BoxConstraints.tight(Size size)，
            /// 它可以生成给定大小的限制；const BoxConstraints.expand()可以生成一个尽可能大的用以填充另一个容器的BoxConstraints
            constraints: BoxConstraints(
                minWidth: double.infinity, minHeight: 50),
            child: Container(

              height: 5,

              ///虽然给定了高度，但是父级布局设置了最小高度所以失效了
              child: redBox,
            ),
          ),

          ///分割
          Padding(padding: EdgeInsets.symmetric(vertical: 1),),

          ///与 ConstrainedBox 类似，当尺寸固定的时候可以使用这个
          SizedBox(

            width: 60,
            height: 60,
            child: redBox,
          ),

          ///分割
          Padding(padding: EdgeInsets.symmetric(vertical: 1),),

          ///多重限制,的时候取最大值
          ConstrainedBox(
            constraints: BoxConstraints(minWidth: 60, minHeight: 60),
            child: ConstrainedBox(
              constraints: BoxConstraints(minWidth: 90, minHeight: 20),
              child: redBox,),),

          ///分割
          Padding(padding: EdgeInsets.symmetric(vertical: 1),),


          ///接触父级限制,显示大小为子空间的实际宽高：90，30，但是上方会留有70的空白，父级的限制还是生效的，只不过不影响子组件的大小
          ///空白无法去除
          ConstrainedBox(

            constraints: BoxConstraints(minWidth: 60.0, minHeight: 100.0),
            child: UnconstrainedBox(
              child: ConstrainedBox(
                constraints: BoxConstraints(minWidth: 90.0, minHeight: 30.0),
                child: redBox,),
            ),
          )
        ],
      ),
    );
  }
}

